package com.wntms.ft.controller;

import com.github.pagehelper.PageInfo;
import com.wntms.ft.pojo.CarriageContract;
import com.wntms.ft.pojo.CustomerBill;
import com.wntms.ft.pojo.DriverBill;
import com.wntms.ft.pojo.FreightContractBill;
import com.wntms.ft.result.Result;
import com.wntms.ft.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;

    /**
     * 查询用户所有回执单
     * @param currentPage
     * @param pageNumber
     * @return
     */
    @GetMapping("/customer/{currentPage}/{pageNumber}")
    public Result findAllCustomer(@PathVariable int currentPage, @PathVariable int pageNumber){
        List<CustomerBill> allCustomer = billService.findAllCustomer(currentPage, pageNumber);

        if (allCustomer != null) {
            for (CustomerBill customerBill : allCustomer) {
                if (customerBill.getIsDeleted() == 1){
                    customerBill.setStatus("已填");
                }else {
                    customerBill.setStatus("已删除");
                }
            }
            PageInfo<CustomerBill> customerBillPageInfo = new PageInfo<>(allCustomer);
           return Result.success(customerBillPageInfo,"查询成功");
        }
        return Result.error("查询失败");
    }

    /**
     * 删除用户回执单
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteByCustomerId(@PathVariable String id){
        int i = billService.deleteByCustomerId(id);
        if (i == 1) {
           return Result.success(i,"删除成功");
        }else {
          return   Result.error("删除失败");
        }
    }


    @GetMapping("/driver/{currentPage}/{pageNumber}")
    public Result findAllDriver(@PathVariable int currentPage, @PathVariable int pageNumber){

        List<DriverBill> allDriver = billService.findAllDriver(currentPage, pageNumber);

        if (allDriver != null) {
            for (DriverBill driverBill : allDriver) {
                if (driverBill.getIsDeleted() == 1){
                    driverBill.setStatus("已填");
                }else {
                    driverBill.setStatus("已删除");
                }
            }
            PageInfo<DriverBill> driverBillPageInfo = new PageInfo<>(allDriver);
            return Result.success(driverBillPageInfo,"查询成功");
        }
        return Result.error("查询失败");
    }

    /**
     * 删除用户回执单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDriver/{id}")
    public Result deleteByDriverId(@PathVariable int id){
        int i = billService.deleteByDriverId(id);
        if (i == 1) {
            return Result.success(i,"删除成功");
        }else {
            return   Result.error("删除失败");
        }
    }

    /**
     * 查询所有货运单合同
     * @param currentPage
     * @param pageNumber
     * @return
     */
   @GetMapping("/freight/{currentPage}/{pageNumber}")
    public Result findAllFreight(@PathVariable int currentPage, @PathVariable int pageNumber){
       System.out.println(1);
       List<FreightContractBill> allFreightBill = billService.findAllFreightBill(currentPage, pageNumber);
       if (allFreightBill != null) {
           for (FreightContractBill freightContractBill : allFreightBill) {
               if (freightContractBill.getIsDeleted() == 1) {
                   freightContractBill.setStatus("已填");
               }else {
                   freightContractBill.setStatus("已删除");
               }
           }
           PageInfo<FreightContractBill> freightBillPageInfo = new PageInfo<>(allFreightBill);
           return Result.success(freightBillPageInfo,"查询成功");
       }
       return Result.error("查询失败");
    }


    /**
     * 删除货运单合同
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFreight/{id}")
    public Result deleteByFreightId(@PathVariable int id){
        int i = billService.deleteByFreightId(id);
        if (i == 1) {
            return Result.success(i,"删除成功");
        }else {
            return   Result.error("删除失败");
        }
    }



    @GetMapping("/carriage/{currentPage}/{pageNumber}")
    public Result findAllCarriage(@PathVariable int currentPage, @PathVariable int pageNumber){
        List<CarriageContract> allCarriageBill = billService.findAllCarriageBill(currentPage,pageNumber);
        if (allCarriageBill != null) {
            for (CarriageContract carriageContractBill : allCarriageBill) {
                if (carriageContractBill.getIsDeleted() == 1) {
                    carriageContractBill.setReturnStatus("已填");
                }else {
                    carriageContractBill.setReturnStatus("已删除");
                }
            }
            PageInfo<CarriageContract> freightBillPageInfo = new PageInfo<>(allCarriageBill);
            return Result.success(freightBillPageInfo,"查询成功");
        }
        return Result.error("查询失败");
    }


    /**
     * 删除货运单合同
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCarriage/{id}")
    public Result deleteByCarriageId(@PathVariable String id){
        int i = billService.deleteByCarriageId(id);
        if (i == 1) {
            return Result.success(i,"删除成功");
        }else {
            return   Result.error("删除失败");
        }
    }




}
